var contextPath = "/boot-crm";
$(document).ready(function () {
    var check=document.getElementById("checkbox");
    var submit=document.getElementById("submit");

    submit.className="submit-disabled";
    submit.disabled=true;
    check.onclick=function () {
        if(check.checked){
            submit.className="submit";
            submit.disabled=false;

        }else{
            submit.className="submit-disabled";
            submit.disabled=true;
        }
    };

    $("#submit").click(function () {
        $.ajax({
            type: "POST",
            url: contextPath+"/user/register.action",
            contentType:"application/json; charset=utf-8",
            data:JSON.stringify(
                {
                    "user_code": $("#account").val(),
                    "password": $("#password").val(),
                    "password2":  $("#re").val(),
                    "name":  $("#name").val()
                }
            ),
            dataType:"json",
            success:function (data) {
                var msg = document.getElementsByClassName("msg");

                if(data.hasOwnProperty("user_code")){
                    msg[0].style.visibility = "visible";
                    msg[0].style.background="url("+contextPath+"/img/error.png) no-repeat 4px center";
                    msg[0].innerHTML=data.user_code;
                } else {
                    msg[0].style.visibility = "hidden";
                }
                if(data.hasOwnProperty("password")){
                    msg[1].style.visibility = "visible";
                    msg[1].style.background="url("+contextPath+"/img/error.png) no-repeat 4px center";
                    msg[1].innerHTML=data.password;
                } else {
                    msg[1].style.visibility = "hidden";
                }
                if(data.hasOwnProperty("password2")){
                    msg[2].style.visibility = "visible";
                    msg[2].style.background="url("+contextPath+"/img/error.png) no-repeat 4px center";
                    msg[2].innerHTML=data.password2;
                } else {
                    msg[2].style.visibility = "hidden";
                }
                if(data.hasOwnProperty("name")){
                    msg[3].style.visibility = "visible";
                    msg[3].style.background="url("+contextPath+"/img/error.png) no-repeat 4px center";
                    msg[3].innerHTML=data.name;
                } else {
                    msg[3].style.visibility = "hidden";
                }
                if (data.hasOwnProperty("result")) {
                    // setTimeout(function () {
                    window.location.href=contextPath+"/login.action"
                    // },2000);
                }
            }
        })
    });
    $("#account").blur(function () {
        restoreStyle("account-div");
        userIsExist();
    });

});


function userIsExist() {
    $.ajax({
        type: "POST",
        url: contextPath+"/user/userIsExist.action",
        contentType:"application/json; charset=utf-8",
        data:JSON.stringify({"user_code":$("#account").val()}),
        dataType:"json",
        success:function (data) {
            var msg = document.getElementsByClassName("msg")[0];
            if(document.getElementById("account").value === ""){
                msg.style.visibility = "visible";
                msg.style.background="url("+contextPath+"/img/error.png) no-repeat 4px center";
                msg.innerHTML = "请输入用户名！";
            }else if (data.result === true) {
                msg.style.visibility = "visible";
                msg.style.background="url("+contextPath+"/img/error.png) no-repeat 4px center";
                msg.innerHTML = "用户名已存在！";
            }else{
                msg.style.visibility = "visible";
                msg.style.background="url("+contextPath+"/img/reg_succ.png) no-repeat 4px center";
                msg.innerHTML = "用户名可用！";
            }
        }
    })
}

//获得焦点时改变样式
function changeStyle(div) {
    $("#"+div).attr("class","div-change");
    // var odiv=document.getElementById(div);
    // odiv.className="div-change";
}
//失去焦点时改变样式
function restoreStyle(div) {
    $("#"+div).attr("class","div-default");
    // var odiv=document.getElementById(div);
    // odiv.className="div-default";
}